//
//  DataBaseSaveEditeImageModel.m
//  WuliHanhan
//
//  Created by tang on 16/3/16.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "DataBaseSaveEditeImageModel.h"
#import "Header.h"

#import "FMDB.h"

@interface DataBaseSaveEditeImageModel ()
{
    FMDatabase *db;
//    NSString *fileString;
    
}

@end




@implementation DataBaseSaveEditeImageModel


sloganIntence(DataBaseSaveEditeImageModel);







////插入数据
//- (void)insertImage:(NSData *)data withIndex:(NSString *)indexString
//{
//    
//        BOOL writeIsOK = [db executeUpdate:@"INSERT INTO EditeImageList (ImageName, CellIndex) VALUES (?,?)", data, indexString];
//        
////    });
//   
//    
//    if (writeIsOK)
//    {
////        NSLog(@"成功");
//    }
//    else
//    {
////        NSLog(@"SHIBAI");
//    }
//}


//删除
- (void)deletaIMage:(NSString *)indesString
{
    BOOL deletaOK = [db executeUpdate:@"DELETE FROM EditeImageList WHERE CellIndex = ?",indesString];
    
    if (deletaOK)
    {
        NSLog(@"deletechenggong");
    }
    else
    {
        NSLog(@"deleteerror");
    }
}



#pragma mark----插入数据
- (void)insertImageWithInfo:(NSDictionary *)dic withImage:(NSData *)data withImageName:(NSString *)nameString
{
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    BOOL writeIsOK = [db executeUpdate:@"INSERT INTO EditeImageList (ImageData, CellIndex, ImageDic) VALUES (?,?,?)", data, nameString, infoData];
    
    if (writeIsOK)
    {
        NSLog(@"添加成功");
    }
    else
    {
        NSLog(@"添加失败");
    }
}




//获取相册数据库的照片
- (void)rearDataBaseForPhotos:(getDataModel)callBack
{
    NSMutableArray *dataArray = [NSMutableArray new];
    NSMutableArray *nameAY = [NSMutableArray new];
    
    FMResultSet *rs = [db executeQuery:@"SELECT CellIndex , ImageData FROM EditeImageList"];
    
//    NSLog(@"%@", rs);
    
    while ([rs next]) {
         NSString *name = [rs stringForColumn:@"CellIndex"];
        [nameAY addObject:name];
//        NSLog(@"")
         NSData *data = [rs dataForColumn:@"ImageData"];
        [dataArray addObject:data];
     }

//    NSLog(@"da%@", nameAY);
    callBack(nameAY, dataArray);
//    [self closeDataBase];
}



#pragma mark---------获取数据库的照片

//更新图片信息
- (void)updateImageInfo:(NSDictionary *)info withCellindex:(NSString *)currentIndex
{
    NSLog(@"%@\n%@", info, currentIndex);
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:nil];
    BOOL rs = [db executeUpdate:@"UPDATE EditeImageList SET ImageDic = ? WHERE CellIndex = ?", data, currentIndex];
//    FMResultSet *rs = [db executeUpdate:@"UPDATE EditeImageList SET ImageDic = ? WHERE CellIndex = ?" data,currentIndex];
    if (rs)
    {
        NSLog(@"success");
    }
    else
    {
        NSLog(@"error");
    }
//
}



//拍照结束添加图片index加1
- (void)updateDataBaseWithIndex
{
    [db executeUpdate:@"UPDATE EditeImageList SET CellIndex = CellIndex + 1"];
}






#pragma mark-----new


//打开数据库
- (void)openDataBase
{
    
    //创建编辑图片数据库
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    //    NSLog(@"%@", documentDirectory);
    NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"MyDatabase.db"];
    db = [FMDatabase databaseWithPath:dbPath] ;
    if (![db open]) {
        NSLog(@"Could not open db");
        //        return ;
    }
    else
    {
        [db executeUpdate:@"DROP TABLE IF EXISTS EditeImageList"];
        [db executeUpdate:@"CREATE TABLE IF NOT EXISTS EditeImageList (ImageData blob, CellIndex integer, CropeData blob, qipaoDic blob, postView blob, withConnectID integer, yssuoIMageData blob)"];
    }
    
}


//写入图片信息
- (void)writeImageData:(NSData *)imageData withCropData:(NSData *)data withTopView:(NSDictionary *)dic withImageIndexName:(NSString *)name withPostImage:(NSData *)postData withConnectID:(NSString *)connectIDString withYasuo:(NSData *)yssuoData

{
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    
//    NSLog(@"%@", postData);
    
    BOOL writeIsOK = [db executeUpdate:@"INSERT INTO EditeImageList (ImageData, CellIndex, CropeData, qipaoDic, postView, withConnectID, yssuoIMageData) VALUES (?,?,?,?,?,?,?)", imageData, name, data, infoData, postData, connectIDString, yssuoData];
    
    if (writeIsOK)
    {
        NSLog(@"添加成功");
    }
    else
    {
        NSLog(@"添加失败");
    }

}


//写入裁剪图片
- (void)writeCropeImage:(NSData *)data witnName:(NSString *)name
{
    
    BOOL rs = [db executeUpdate:@"UPDATE EditeImageList SET CropeData = ? WHERE CellIndex = ?", data, name];
    //    FMResultSet *rs = [db executeUpdate:@"UPDATE EditeImageList SET ImageDic = ? WHERE CellIndex = ?" data,currentIndex];
    if (rs)
    {
        NSLog(@"success");
    }
    else
    {
        NSLog(@"error");
    }

}


//写入气泡图片裁剪信息
- (void)writeCropIMage:(NSData *)imageData withQipaoInfo:(NSDictionary *)infoDic witnName:(NSString *)name
{
    NSData *infoData = [NSJSONSerialization dataWithJSONObject:infoDic options:NSJSONWritingPrettyPrinted error:nil];
    
//    NSLog(@"-----------%@", imageData);
    BOOL rs = [db executeUpdate:@"UPDATE EditeImageList SET postView = ?, qipaoDic = ? WHERE CellIndex = ?", imageData, infoData, name];
    //    FMResultSet *rs = [db executeUpdate:@"UPDATE EditeImageList SET ImageDic = ? WHERE CellIndex = ?" data,currentIndex];
    if (rs)
    {
        NSLog(@"111success");
    }
    else
    {
        NSLog(@"error");
    }

}

//删除照片
- (void)deleteSelectImageWithName:(NSString *)name
{
    BOOL deletaOK = [db executeUpdate:@"DELETE FROM EditeImageList WHERE CellIndex = ?",name];
    
    if (deletaOK)
    {
        NSLog(@"deletechenggong");
    }
    else
    {
        NSLog(@"deleteerror");
    }

}


- (void)rearDataBaseImageWithName:(NSString *)nsme andWriteIMageWithlast:(NSString *)lastIMageName getImage:(getImage)callBack
{
    //写入上一张照片
    
        NSLog(@"%@", nsme);
    //读取点击照片
    FMResultSet *rs = [db executeQuery:@"SELECT ImageData ,CropeData, qipaoDic FROM EditeImageList WHERE CellIndex = ?", nsme];
    while ([rs next]) {
        
        NSData *data = [rs dataForColumn:@"ImageData"];
        NSData *data2 = [rs dataForColumn:@"CropeData"];
        NSData *data3 = [rs dataForColumn:@"qipaoDic"];
//        NSData *data4 = [rs dataForColumn:@"postView"];
//        NSLog(@"++++++++%@", data4);
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data3 options:NSJSONReadingMutableContainers error:nil];

        callBack(data, data2, dictionary);
        
    }
    
}

//获取头部压缩图
- (void)readYaSuoImage:(getYaSuo)callBack
{
    FMResultSet *rs = [db executeQuery:@"SELECT CellIndex FROM EditeImageList"];
    
    while ([rs next])
    {
        NSString *nameString = [rs stringForColumn:@"CellIndex"];
//        callBack(nameString);
        FMResultSet *rs2 = [db executeQuery:@"SELECT yssuoIMageData FROM EditeImageList WHERE CellIndex = ?", nameString];
        if ([rs2 next]) {
            NSData *data = [rs2 dataForColumn:@"yssuoIMageData"];
            callBack(data, nameString);
            
        }
        
    }

}




- (void)readUpLoadImageWithName:(NSString *)name withCallBack:(getUpLoadImageData)callBack
{
    FMResultSet *rs = [db executeQuery:@"SELECT postView , CropeData, withConnectID FROM EditeImageList WHERE CellIndex = ?", name];
    if ([rs next])
    {
        NSLog(@"success");

        NSData *data = [rs dataForColumn:@"postView"];
        NSData *data2 = [rs dataForColumn:@"CropeData"];
//        NSLog(@"%@", data);
        NSString *IDString = [NSString stringWithFormat:@"%d", [rs intForColumn:@"withConnectID"]];
        callBack(data, data2, IDString);
    }
    else
    {
        NSLog(@"shibai");
    }
}



#pragma mark-----






- (void)closeDataBase
{
    [db close];
}






@end
